/**
 * @file test_freertos_api.c
 * @brief FreeRTOS API兼容性验证测试
 * 
 * 验证WorkQueue所需的FreeRTOS API是否可用
 */

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include <string.h>

// External debug printf function
extern void debug_printf(const char *format, ...);

// 测试计数器
static int test_passed = 0;
static int test_failed = 0;

#define TEST_ASSERT(condition, msg) \
    do { \
        if (condition) { \
            test_passed++; \
            debug_printf("  [PASS] %s\r\n", msg); \
        } else { \
            test_failed++; \
            debug_printf("  [FAIL] %s\r\n", msg); \
        } \
    } while(0)

// 测试任务函数
static volatile int task_run_count = 0;
static void test_task_func(void *arg) {
    int *counter = (int *)arg;
    (*counter)++;
    task_run_count++;
    vTaskDelete(NULL);
}

// 测试互斥锁
static SemaphoreHandle_t test_mutex = NULL;
static volatile int mutex_protected_value = 0;

static void mutex_test_task(void *arg) {
    for (int i = 0; i < 10; i++) {
        xSemaphoreTake(test_mutex, portMAX_DELAY);
        mutex_protected_value++;
        xSemaphoreGive(test_mutex);
        vTaskDelay(pdMS_TO_TICKS(1));
    }
    vTaskDelete(NULL);
}

/**
 * @brief 运行FreeRTOS API兼容性测试
 */
void freertos_api_run_tests(void) {
    debug_printf("\r\n========================================\r\n");
    debug_printf("  FreeRTOS API Compatibility Tests\r\n");
    debug_printf("========================================\r\n\r\n");

    test_passed = 0;
    test_failed = 0;

    // ========================================
    // Test 1: Task Creation
    // ========================================
    debug_printf("Test 1: Task Creation\r\n");
    {
        TaskHandle_t task_handle = NULL;
        int counter = 0;
        task_run_count = 0;

        BaseType_t ret = xTaskCreate(test_task_func, "test_task", 
                                      256, &counter, 5, &task_handle);
        TEST_ASSERT(ret == pdPASS, "xTaskCreate returns pdPASS");
        TEST_ASSERT(task_handle != NULL, "Task handle is not NULL");

        // 等待任务执行
        vTaskDelay(pdMS_TO_TICKS(50));
        TEST_ASSERT(counter == 1, "Task executed once");
        TEST_ASSERT(task_run_count == 1, "Task run count is 1");
    }

    // ========================================
    // Test 2: Task Name
    // ========================================
    debug_printf("\r\nTest 2: Task Name\r\n");
    {
        TaskHandle_t task_handle = NULL;
        int counter = 0;

        xTaskCreate(test_task_func, "named_task", 256, &counter, 5, &task_handle);
        
        const char *name = pcTaskGetName(task_handle);
        TEST_ASSERT(name != NULL, "pcTaskGetName returns non-NULL");
        TEST_ASSERT(strcmp(name, "named_task") == 0, "Task name is correct");

        vTaskDelay(pdMS_TO_TICKS(50));
    }

    // ========================================
    // Test 3: Mutex Creation and Operations
    // ========================================
    debug_printf("\r\nTest 3: Mutex Creation and Operations\r\n");
    {
        SemaphoreHandle_t mutex = xSemaphoreCreateMutex();
        TEST_ASSERT(mutex != NULL, "xSemaphoreCreateMutex returns non-NULL");

        BaseType_t ret = xSemaphoreTake(mutex, portMAX_DELAY);
        TEST_ASSERT(ret == pdTRUE, "xSemaphoreTake succeeds");

        ret = xSemaphoreGive(mutex);
        TEST_ASSERT(ret == pdTRUE, "xSemaphoreGive succeeds");

        vSemaphoreDelete(mutex);
        TEST_ASSERT(1, "vSemaphoreDelete completes");
    }

    // ========================================
    // Test 4: Mutex Thread Safety
    // ========================================
    debug_printf("\r\nTest 4: Mutex Thread Safety\r\n");
    {
        test_mutex = xSemaphoreCreateMutex();
        mutex_protected_value = 0;

        TaskHandle_t task1 = NULL, task2 = NULL;
        xTaskCreate(mutex_test_task, "mutex_task1", 256, NULL, 5, &task1);
        xTaskCreate(mutex_test_task, "mutex_task2", 256, NULL, 5, &task2);

        // 等待两个任务完成
        vTaskDelay(pdMS_TO_TICKS(200));

        TEST_ASSERT(mutex_protected_value == 20, "Mutex protected value is 20");

        vSemaphoreDelete(test_mutex);
    }

    // ========================================
    // Test 5: Task Priority
    // ========================================
    debug_printf("\r\nTest 5: Task Priority\r\n");
    {
        TaskHandle_t task_handle = NULL;
        int counter = 0;

        xTaskCreate(test_task_func, "prio_task", 256, &counter, 10, &task_handle);
        
        UBaseType_t priority = uxTaskPriorityGet(task_handle);
        TEST_ASSERT(priority == 10, "Task priority is 10");

        vTaskDelay(pdMS_TO_TICKS(50));
    }

    // ========================================
    // 测试总结
    // ========================================
    debug_printf("\r\n========================================\r\n");
    debug_printf("  Test Summary\r\n");
    debug_printf("========================================\r\n");
    debug_printf("Total tests: %d\r\n", test_passed + test_failed);
    debug_printf("Passed: %d\r\n", test_passed);
    debug_printf("Failed: %d\r\n", test_failed);
    
    if (test_failed == 0) {
        debug_printf("\r\n✅ All FreeRTOS API tests PASSED!\r\n");
        debug_printf("✅ FreeRTOS native API is fully compatible\r\n");
        debug_printf("✅ Ready to implement WorkQueue with FreeRTOS API\r\n");
    } else {
        debug_printf("\r\n❌ Some tests FAILED!\r\n");
    }
    debug_printf("========================================\r\n\r\n");
}

